package com.tankgame;

import java.util.Vector;

/**
 * @author 周超
 * @version 1.0
 * 自己的坦克
 */
@SuppressWarnings({"all"})
public class Hero extends Tank
{

    //让我方坦克hero得到所有的敌人坦克，也就是敌人坦克集合
    Vector<EnemyTank> enemyTanks = new Vector<>();
    public void setEnemyTanks(Vector<EnemyTank> enemyTanks)
    {
        this.enemyTanks = enemyTanks;
    }

    public Hero(int x, int y)
    {
        super(x, y);
    }

    //定义一个Shot对象，表示一个射击行为（线程）
    Shot shot = null;//子弹的线程消亡，并不意味着Shot对象是空的，Shot对象只要被创建，就不会为空
    //让我方坦克可以发射多颗子弹
    Vector<Shot> shots = new Vector<>();
    //当按下 J键的时候，就会调用shotEnemyTank()方法，在这个方法里，就会启动shot线程
    //射击方法
    public void shotEnemyTank()
    {
        //创建Shot对象，根据当前的 Hero对象的位置和方向，来创建Shot对象
        switch (getDirect()) //这里是得到我方坦克的方向
        {
            case 0:  //向上发射子弹
                shot = new Shot(getX() + 20, getY(), 0);
                break;
            case 1:  //向右发射子弹
                shot = new Shot(getX() + 60, getY() + 20, 1);
                break;
            case 2:  //向下发射子弹
                shot = new Shot(getX() + 20, getY() + 60, 2);
                break;
            case 3:  //向左发射子弹
                shot = new Shot(getX(), getY() + 20, 3);
                break;
        }

        //控制在我们的面板上，最多只有5颗子弹
        if(shots.size() == 5)
        {
            return;
        }
        //把新创建好的shot对象添加到Vector<Shot> shots
        shots.add(shot);

        //创建好Shot对象后，启动Shot线程
        new Thread(shot).start();
    }


    //编写方法：防止我方坦克主动和敌人坦克重叠
    //判断我方坦克，是否和集合enemyTanks中的其它坦克发生了重叠或者是碰撞
    public  boolean isTouch() //返回true代表发生碰撞，反之亦然，
    // 在想一想这个方法应该放在哪里合适？ ————>我方坦克移动的时候
    {
        //判断我方坦克(this)的方向
        switch (this.getDirect()) {
            case 0:  //我方坦克向上
                //for循环，把所有的敌人坦克都取出来，让当前敌人坦克跟所有坦克进行比较
                for (int i = 0; i < enemyTanks.size(); i++)
                {
                    //从Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);

                    //如果敌人坦克是上/下
                    //老韩分析
                    //1.如果敌人坦克是上/下 x的范围 [enemyTank.getX(), enemyTank.getX() + 40]
                    //                      y的范围 [enemyTank.getY(), enemyTank.getY() + 60]

                    //如果敌人坦克是上/下
                    if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2)
                    {
                        //2. 当前我方坦克左上角的坐标 [this.getX(), this.getY()]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60)
                        {
                            return true;
                        }
                        //3. 当前我方坦克右上角的坐标 [this.getX() + 40, this.getY()]
                        if (this.getX() + 40 >= enemyTank.getX()
                                && this.getX() + 40 <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60)
                        {
                            return true;
                        }
                    }

                    //如敌人坦克是右/左
                    //1.如果敌人坦克是右/左 x的范围 [enemyTank.getX(), enemyTank.getX() + 60]
                    //                      y的范围 [enemyTank.getY(), enemyTank.getY() + 40]
                    if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3)
                    {
                        //2. 当前我方坦克左上角的坐标 [this.getX(), this.getY()]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40)
                        {
                            return true;
                        }

                        //3. 当前我方坦克右上角的坐标 [this.getX() + 40, this.getY()]
                        if (this.getX() + 40 >= enemyTank.getX()
                                && this.getX() + 40 <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40)
                        {
                            return true;
                        }
                    }

                }
                break;


            case 1:  //我方坦克向右
                //for循环，把所有的敌人坦克都取出来，让当前敌人坦克跟所有坦克进行比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);


                    //如果敌人坦克是上/下
                    //老韩分析
                    //1.如果敌人坦克是上/下 x的范围 [enemyTank.getX(), enemyTank.getX() + 40]
                    //                      y的范围 [enemyTank.getY(), enemyTank.getY() + 60]

                    //如果敌人坦克是上/下
                    if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                        //2. 当前坦克右上角的坐标 [this.getX() + 60, this.getY()]
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //3. 当前坦克右下角的坐标 [this.getX() + 60, this.getY() + 40]
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 40
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 60) {
                            return true;
                        }
                    }

                    //如果敌人坦克是右/左
                    //1.如果敌人坦克是右/左 x的范围 [enemyTank.getX(), enemyTank.getX() + 60]
                    //                      y的范围 [enemyTank.getY(), enemyTank.getY() + 40]
                    if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                        //2. 当前坦克右上角的坐标 [this.getX() + 60, this.getY()]
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40) {
                            return true;
                        }

                        //3. 当前坦克右下角的坐标 [this.getX() + 60, this.getY() + 40]
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 60
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 40) {
                            return true;
                        }
                    }

                }
                break;
            case 2:  //我方坦克向下
                //for循环，把所有的敌人坦克都取出来，让当前敌人坦克跟所有坦克进行比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //如果敌人坦克是上/下
                    //老韩分析
                    //1.如果敌人坦克是上/下 x的范围 [enemyTank.getX(), enemyTank.getX() + 40]
                    //                      y的范围 [enemyTank.getY(), enemyTank.getY() + 60]

                    //如果敌人坦克是上/下
                    if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                        //2. 当前坦克左下角的坐标 [this.getX(), this.getY() + 60]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //3. 当前坦克右下角的坐标 [this.getX() + 40, this.getY() + 60]
                        if (this.getX() + 40 >= enemyTank.getX()
                                && this.getX() + 40 <= enemyTank.getX() + 40
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 60) {
                            return true;
                        }
                    }

                    //如果敌人坦克是右/左
                    //1.如果敌人坦克是右/左 x的范围 [enemyTank.getX(), enemyTank.getX() + 60]
                    //                      y的范围 [enemyTank.getY(), enemyTank.getY() + 40]
                    if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                        //2. 当前坦克左下角的坐标 [this.getX(), this.getY() + 60]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 40) {
                            return true;
                        }

                        //3. 当前坦克右下角的坐标 [this.getX() + 40, this.getY() + 60]
                        if (this.getX() + 40 >= enemyTank.getX()
                                && this.getX() + 40 <= enemyTank.getX() + 60
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 40) {
                            return true;
                        }
                    }

                }

                break;
            case 3:  //我方坦克向左

                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从Vector 中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //如果敌人坦克是上/下
                    //老韩分析
                    //1. 如果敌人坦克是上/下 x的范围 [enemyTank.getX(), enemyTank.getX() + 40]
                    //                    y的范围 [enemyTank.getY(), enemyTank.getY() + 60]

                    if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                        //2. 当前坦克 左上角的坐标 [this.getX(), this.getY() ]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //3. 当前坦克 左下角的坐标 [this.getX(), this.getY() + 40]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 60) {
                            return true;
                        }
                    }
                    //如果敌人坦克是 右/左
                    //老韩分析
                    //1. 如果敌人坦克是右/左  x的范围 [enemyTank.getX(), enemyTank.getX() + 60]
                    //                     y的范围 [enemyTank.getY(), enemyTank.getY() + 40]
                    if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                        //2. 当前坦克 左上角的坐标 [this.getX(), this.getY() ]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40) {
                            return true;
                        }
                        //3. 当前坦克 左下角的坐标 [this.getX(), this.getY() + 40]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 40) {
                            return true;
                        }
                    }

                }
                break;
        }
        return false;
    }






}







